import type { IDept } from '@/types/dept'
import type { ILogin } from '@/types/user'
import request from './request'

export function login(data: ILogin) {
  return request({
    url: '/sys/login',
    method: 'post',
    data
  })
}

export function getMenuTree() {
  return request({
    url: '/sys/menu/nav',
    method: 'get',
    loading: true
  })
}
export function imgUpload(formData: any) {
  return request({
    url: '/sys/file/upload',
    method: 'post',
    data: formData
  })
}
export function getCurrentInfo() {
  return request({
    url: '/sys/user/info',
    method: 'get',
    loading: true
  })
}

export function getRoleList() {
  return request({
    url: '/admin/system/role/list',
    method: 'get',
    loading: true
  })
}

export function modifyPassword(data: { password: string; newPassword: string }) {
  return request({
    url: '/sys/user/password',
    method: 'post',
    loading: true,
    headers: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    data
  })
}

export function getDeptTree() {
  return request({
    url: '/sys/dept/list',
    method: 'get'
  }).then(({ deptList }) => {
    return deptList.map((dept: IDept) => {
      const { name, deptId, parentId } = dept
      return {
        title: name,
        id: deptId,
        value: deptId,
        pId: parentId
      }
    })
  })
}